//
//  LogisticsInfoVC.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2021/6/11.
//  Copyright © 2021 zwb. All rights reserved.
//

import UIKit

class ZyaLogisticsInfoVC: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    var model = ZyaLogisticsModel(JSON:["":""])!
     var details = [LoInfoModel]()
    
    
    lazy var tableView:UITableView = {
        let v = UITableView(frame: self.view.bounds)
        v.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 100, right: 0)
        v.delegate = self
        v.dataSource = self
        return v
    }()
    
    var headerStr1 = ""
    var headerStr2 = ""
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "物流信息"
        self.view.addSubview(self.tableView)
        
        self.tableView.estimatedRowHeight = 40
        self.tableView.rowHeight = UITableView.automaticDimension
        self.tableView.separatorStyle = .none
        
        self.tableView.backgroundColor = UIColor(kshex: "#f2f2f2")
        self.tableView.register(ZyaLogisticsInfoCell.self, forCellReuseIdentifier: ZyaLogisticsInfoCell.identifier)
        self.tableView.register(ZyaLoInfoHeader.self, forHeaderFooterViewReuseIdentifier: ZyaLoInfoHeader.identifier)
         self.tableView.tableFooterView = UIView()
        
        KSNetwork.shared.doRequest(API.queryLogisticsDetail(z_logistics_no: model.zLogisticsNo.unwrap,z_logistics_nm: model.zLogisticsNm.unwrap,z_consigner_tel: model.zConsignerTel.unwrap)) { (data:BaseResult<ZyaLogisticsModel>) in
            if data.flag == TRUE {
                if let infos_o = data.infos_object {
                    if let info = infos_o.info_object {
                        if let res = info.result {
                            self.headerStr1 = res.expName.unwrap
                            self.headerStr2 = self.model.zLogisticsNo.unwrap
                            if let list = res.list {
                                self.details =  list
                            }
                        }
                    }
                }
                self.tableView.reloadData()
            } else {
                KSToast(data.message)
            }
        }
        
        
    }
    
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: ZyaLogisticsInfoCell.identifier, for: indexPath) as? ZyaLogisticsInfoCell else { 
            fatalError()
        }
        
        cell.one = self.details[indexPath.row]
        if indexPath.row == 0 {
            cell.line1.isHidden = true
        } else {
            cell.line1.isHidden = false
        }
        if indexPath.row == self.details.count - 1 {
            cell.line2.isHidden = true
        } else {
            cell.line2.isHidden = false
        }
        return cell
        
        
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.details.count
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 90
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
         
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: ZyaLoInfoHeader.identifier) as! ZyaLoInfoHeader
        header.headerLab1.text = self.headerStr1
        header.headerLab2.text = self.headerStr2

        return header
        
        
         
         
    }
 
    
    
}

class ZyaLoInfoHeader: UITableViewHeaderFooterView {
    lazy var headerLab1:UILabel = {
        let v = UILabel()
        return v
    }()
    lazy var headerLab2:UILabel = {
        let v = UILabel()
        return v
    }()
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        
        let contentVWidth = KSGlobal.screenW
        self.contentView.backgroundColor = .clear
        let bg = UIView(frame: CGRect(x:5,y:5,width: contentVWidth - 10,height: 80))
        bg.layer.masksToBounds = true
        bg.layer.cornerRadius = 6
        self.contentView.addSubview(bg)
        bg.backgroundColor = .white
        headerLab1.frame = CGRect(x:5,y:3,width: contentVWidth - 10,height: 35)
        bg.addSubview(headerLab1)
        headerLab2.frame = CGRect(x:5,y:40,width: contentVWidth - 10,height: 35)
        bg.addSubview(headerLab2)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
 
}
